<template>
  <view>
    <view class="my-search-box">
      <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索列表 -->
    <view class="sugg-list" v-if="searchList.length !== 0">
      <view class="sugg-item" v-for="(item,i) in searchList.goods" :key="i" @click="goteDetail(item.goods_id)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash-filled" size="16" @click="clearHistory"></uni-icons>
      </view>
      <view class="history-content">
        <view class="history-item" v-for="(item,i) in histories" :key="i" @click="historyGotolist(item)">
          {{item}}
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        kw:'',
        searchList:[],
        historyList:[]
      };
    },
    methods:{
      goteDetail(id){
        console.log(id);
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+id
        })
      },
      input(e){
        clearTimeout(this.timer)
        this.timer = setTimeout(()=>{
          this.kw = e
          this.getSearchList()
          
        },500)  
      },
      async getSearchList(){
        if(this.kw === ''){
          this.searchList = []
          return
          }
        const {data:res} = await uni.$http.get('https://api-ugo-web.itheima.net/api/public/v1/goods/search',{query:this.kw})
        if(res.meta.status !== 200) return uni.$showMsg()
        this.searchList = res.message
        // this.historyList.forEach((item)=>{
        //   if(item !== this.kw) 
        //   this.historyList.push(this.kw)
        // })
        
        
      },
      clearHistory(){
        this.historyList=[]
        uni.clearStorageSync('kw')
      },
      historyGotolist(value){
        console.log('ok');
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+value
        })
      }
      
    },
    computed:{
      histories(){
        return [...this.historyList].reverse()
      }
    },
    watch:{
      kw(value){
        if(this.historyList.indexOf(value) === -1 && value !== ''){
          this.historyList.push(value) 
        }else if(this.historyList.indexOf(value) !== -1){
          this.historyList.splice(this.historyList.indexOf(value),1)
          this.historyList.push(value)
        }
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      }
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    }
  }
</script>

<style lang="scss">
.my-search-box{
  height: 50px;
  background-color: #c00000;
  position: sticky;
  top: 0;
  z-index: 999;
  uni-search-bar{
  } 
}
.sugg-list{
  padding:0 5px;
  
  .sugg-item{
      display:flex;
      align-items:center;
      justify-content:space-between;
      font-size: 12px;
      padding:13px 0;
      border-bottom:1px solid #eeeeee;
      
      .goods-name{
        white-space:nowrap;
        overflow:hidden;
        text-overflow:ellipsis;
      }
  }
}
.history-box{
  .history-title{
    display:flex;
    align-items: center;
    justify-content: space-between;
    margin: 5px;
    font-size: 12px;
  }
  .history-content{
    display: flex;
    font-size: 12px;
    align-items: center;
    flex-wrap: wrap;
    .history-item{
      margin: 5px;
      background-color: #eeeeee;
      border-radius: 5px;
      padding: 0 5px;
    }
  }
}
</style>
